<template>
  <div>
    <el-form
      :inline="true"
      size="small"
      ref="tableData"
      :model="tableData"
      class="demo-form-inline"
    >
      <el-form-item prop="id">
        <el-input
          v-model="tableData.id"
          clearable
          @clear="getSupplierList"
          placeholder="供应商ID"
        ></el-input>
      </el-form-item>
      <el-form-item prop="name">
        <el-input v-model="tableData.name" placeholder="供应商名称"></el-input>
      </el-form-item>
      <el-form-item prop="linkman">
        <el-input v-model="tableData.linkman" placeholder="联系人"></el-input>
      </el-form-item>
      <el-form-item prop="mobile">
        <el-input v-model="tableData.mobile" placeholder="联系电话"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button type="primary" @click="addDialog">新增</el-button>
        <el-button @click="resetting('tableData')">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="supplierList" border style="width: 100%">
      <el-table-column type="index" label="序号" width="60"> </el-table-column>
      <el-table-column prop="name" label="供应商名称"> </el-table-column>
      <el-table-column prop="linkman" label="联系人"> </el-table-column>
      <el-table-column prop="mobile" label="联系电话"> </el-table-column>
      <el-table-column prop="remark" label="备注"> </el-table-column>
      <el-table-column label="操作" width="180">
        <template slot-scope="scope">
          <el-button size="mini" @click="getDialog(scope.row.id)"
            >编辑</el-button
          >
          <el-button size="mini" type="danger" @click="delSupplier(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[7, 10, 15, 20]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <el-dialog :title="title" :visible.sync="dialogVisible" width="450px">
      <span>
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="供应商名称" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="联系人" prop="linkman">
            <el-input v-model="ruleForm.linkman"></el-input>
          </el-form-item>
          <el-form-item label="联系电话" prop="mobile">
            <el-input v-model="ruleForm.mobile"></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" v-model="ruleForm.remark"></el-input>
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resettingDia('ruleForm')">取 消</el-button>
        <el-button
          type="primary"
          v-if="title == '新增供应商'"
          @click="addSupplier"
          >确 定</el-button
        >
        <el-button
          type="primary"
          v-if="title == '编辑供应商'"
          @click="editSupplier"
          >编 辑</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import supplierApi from "../../api/supplier";
export default {
  data() {
    return {
      tableData: {
        name: "",
        linkman: "",
        mobile: "",
        remark: "",
      },
      page: 1,
      size: 7,
      total: 0,
      supplierList: [],
      ruleForm: {
        name: "",
        linkman: "",
        mobile: "",
        remark: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入供应商名称", trigger: "blur" },
        ],
        linkman: [{ required: true, message: "请输入联系人", trigger: "blur" }],
        mobile: [
          { required: true, message: "请输入联系电话", trigger: "blur" },
        ],
        remark: [{ required: true, message: "请输入备注", trigger: "blur" }],
      },
      dialogVisible: false,
      title: "",
    };
  },
  created() {
    this.getSupplierList();
  },
  mounted() {},
  methods: {
    async getSupplierList() {
      try {
        let res = await supplierApi.getSupplier(
          this.page,
          this.size,
          this.tableData
        );
        if (res.data.flag) {
          this.supplierList = res.data.data.rows;
          this.total = res.data.data.total;
        } else {
          this.$message.error(res.data.message);
        }
      } catch {
        this.$message.error("请求发生错误");
      }
    },
    handleSizeChange(val) {
      this.size = val;
      this.getSupplierList();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getSupplierList();
    },
    search() {
      this.getSupplierList();
    },
    resetting(formName) {
      this.$refs[formName].resetFields();
    },
    addDialog() {
      this.title = "新增供应商";
      this.dialogVisible = true;
    },
    resettingDia(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false;
    },
    async addSupplier() {
      try {
        let res = await supplierApi.addSupplier(this.ruleForm);
        if (res.data.flag) {
          this.$message.success(res.data.message);
          this.getSupplierList();
          this.$refs["ruleForm"].resetFields();
          this.dialogVisible = false;
        } else {
          this.$message.error(res.data.message);
        }
      } catch {
        this.$message.error("请求发生错误");
      }
    },
    async getDialog(id) {
      this.title = "编辑供应商";
      this.dialogVisible = true;
      try {
        let res = await supplierApi.searchSupplier(id);
        if (res.data.flag) {
          this.ruleForm = res.data.data.rows[0];
        } else {
          this.$message.error(res.data.message);
        }
      } catch {
        this.$message.error("请求发生错误");
      }
    },
    async editSupplier() {
      try {
        let res = await supplierApi.editSupplier(this.ruleForm.id, this.ruleForm);
        if (res.data.flag) {
          this.$message.success(res.data.message);
          this.getSupplierList();
          this.$refs["ruleForm"].resetFields();
          this.dialogVisible = false;
        } else {
          this.$message.error(res.data.message);
        }
      } catch {
        this.$message.error("请求发生错误");
      }
    },
    async delSupplier(id) {
      try {
        let res = await supplierApi.delSupplier(id);
        if (res.data.flag) {
          this.$message.success(res.data.message);
          this.getSupplierList();
        } else {
          this.$message.error(res.data.message);
        }
      } catch {
        this.$message.error("请求发生错误");
      }
    }
  },
};
</script>

<style scoped >
.el-form {
  margin-top: 20px;
}
.el-pagination {
  margin-top: 10px;
}
</style>
